package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.MyPagerAdapter;
import com.heer.gymnasium.constant.Extras;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.gymnasium.util.MyImageGetter;
import com.heer.gymnasium.util.SystemUtils;
import com.heer.model.AdSiteBean;
import com.heer.model.SitesBean;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/4/6.
 */

public class SiteIntroductionActivity extends BaseActivity {
    @BindView(R.id.viewpager_scene)
    AutoScrollViewPager viewpagerScene;
    @BindView(R.id.indicator_scene)
    MagicIndicator indicatorScene;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.btn_call)
    ImageView btnCall;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.btn_location)
    ImageView btnLocation;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.rating_level)
    RatingBar ratingLevel;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.tv_desc1)
    TextView tvDesc1;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_desc2)
    TextView tvDesc2;
    @BindView(R.id.tv_name3)
    TextView tvName3;
    @BindView(R.id.tv_desc3)
    TextView tvDesc3;

    private List<View> imageViews = new ArrayList<>(); // 场地图片集合

    private SitesBean.Site mData;

    public static void toSiteIntroduction(Context context, SitesBean.Site data) {
        Intent intent = new Intent();
        intent.setClass(context, SiteIntroductionActivity.class);
        intent.putExtra(Extras.EXTRA_DATA, data);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_site_introduction);
        mData = (SitesBean.Site) getIntent().getSerializableExtra(Extras.EXTRA_DATA);
    }

    @Override
    protected void initView() {
        super.initView();
        initSiteInfo(mData);
    }

    @Override
    protected void loadData() {
        super.loadData();
        mAppHelper.apiService().getSiteAd(mData.getId()).enqueue(new BaseCallback<ApiResponse<List<AdSiteBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<AdSiteBean>>> call, Response<ApiResponse<List<AdSiteBean>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful() && response.body().isSccessful() && response.body().getData().size() > 0) {
                    initSceneViewpager(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<AdSiteBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }

    @OnClick({R.id.btn_call, R.id.btn_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_call:
                SystemUtils.dial(mContext, mData.getStadiumtel());
                break;
            case R.id.btn_location:
                MapActivity.startMap(mContext,tvAddress.getText().toString().trim());
                break;
        }
    }

    private void initSceneViewpager(List<AdSiteBean> data) {

        List<View> imageViews = new ArrayList<>(); // 滑动的图片集合

        for (final AdSiteBean item : data) {
            final ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            Glide.with(mContext).load(LoadImageUtil.getLoadImageUrl(item.getImages())).placeholder(R.mipmap.ic_launcher).into(imageView);
            imageViews.add(imageView);
        }

        CircleNavigator circleNavigator = new CircleNavigator(mContext);
        circleNavigator.setCircleCount(imageViews.size());
        circleNavigator.setCircleColor(Color.WHITE);
        circleNavigator.setCircleClickListener(new CircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                viewpagerScene.setCurrentItem(index);
            }
        });
        indicatorScene.setNavigator(circleNavigator);
        ViewPagerHelper.bind(indicatorScene, viewpagerScene);
        viewpagerScene.setAdapter(new MyPagerAdapter(imageViews));
        viewpagerScene.startAutoScroll();
        viewpagerScene.setBorderAnimation(false);
    }

    private void initSiteInfo(SitesBean.Site mData) {
        tvName.setText(mData.getStadiumname());
        tvAddress.setText("地址：" + mData.getStadiumaddress());
        tvDesc.setText(Html.fromHtml(mData.getStadiumcontent(), new MyImageGetter(mContext, tvDesc), null));
        tvTime.setText("周六日、节假日(" + mData.getStadiumspecialtime() + ")");
        tvTime.append("\n周一至周五(" + mData.getStadiumbusinesstime() + ")");
        tvPhone.setText("电话：" + mData.getStadiumtel());
        ratingLevel.setNumStars(mData.getStar());
        titleBuilder.setTitleText(mData.getStadiumname());
        tvDesc1.setText(Html.fromHtml(mData.getStadiumcontent1(), new MyImageGetter(mContext, tvDesc1), null));
        tvDesc2.setText(Html.fromHtml(mData.getStadiumcontent2(), new MyImageGetter(mContext, tvDesc2), null));
        tvDesc3.setText(Html.fromHtml(mData.getStadiumcontent3(), new MyImageGetter(mContext, tvDesc3), null));
    }
}
